package com.jinmdz.fmis.api.api.service;

import com.jinmdz.fmis.api.base.BaseService;
import com.jinmdz.fmis.api.model.system.UserItem;
import com.jinmdz.fmis.api.wrapper.DictionaryWrapper;
import com.jinmdz.fmis.common.util.BeanUtil;
import com.jinmdz.fmis.common.util.DataUtil;
import com.jinmdz.fmis.common.util.DateUtil;
import com.jinmdz.fmis.core.exception.ActionException;
import com.jinmdz.fmis.dao.dao.ChargeItemDao;
import com.jinmdz.fmis.dao.dao.ChargeSettleDao;
import com.jinmdz.fmis.dao.dao.ChargeSettlePayDao;
import com.jinmdz.fmis.dao.dao.HistoryChargeDao;
import com.jinmdz.fmis.dao.model.charge.ChargeItem;
import com.jinmdz.fmis.dao.model.funeral.FuneralBusinessViewItem;
import com.jinmdz.fmis.dao.model.settlement.ChargeSettlePayItem;
import com.jinmdz.fmis.dao.model.settlement.ChargeSettleViewItem;
import com.jinmdz.fmis.mapper.entity.HistoryChargeEntity;
import com.jinmdz.fmis.mapper.entity.HistoryChargeItemEntity;
import com.jinmdz.fmis.mapper.entity.HistoryChargeSettleEntity;
import com.jinmdz.fmis.mapper.entity.HistoryChargeSettlePayEntity;
import com.jinmdz.fmis.mapper.mapper.HistoryChargeItemMapper;
import com.jinmdz.fmis.mapper.mapper.HistoryChargeMapper;
import com.jinmdz.fmis.mapper.mapper.HistoryChargeSettleMapper;
import com.jinmdz.fmis.mapper.mapper.HistoryChargeSettlePayMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * HistoryChargeService类
 *
 * @author LiCongLu
 * @date 2020-05-13 16:39
 */
@Service("historyChargeService")
public class HistoryChargeService extends BaseService {

    @Resource
    private ChargeSettleDao chargeSettleDao;

    @Resource
    private ChargeItemDao chargeItemDao;

    @Resource
    private ChargeSettlePayDao chargeSettlePayDao;

    @Resource
    private HistoryChargeMapper historyChargeMapper;

    @Resource
    private HistoryChargeSettleMapper historyChargeSettleMapper;

    @Resource
    private HistoryChargeItemMapper historyChargeItemMapper;

    @Resource
    private HistoryChargeSettlePayMapper historyChargeSettlePayMapper;

    @Resource
    private HistoryChargeDao historyChargeDao;

    @Resource
    private DictionaryWrapper dictionaryWrapper;

    /**
     * 保存历史费用记录信息，此函数无事务
     *
     * @param userItem        当前账号
     * @param funeralItem     殡葬业务
     * @param historyNo       历史编号
     * @param chargeSettleIds 结算主键集合
     * @return
     * @author LiCongLu
     * @date 2020-05-14 08:55
     */
    public void saveHistoryCharge(UserItem userItem, FuneralBusinessViewItem funeralItem, String historyNo, ArrayList<Integer> chargeSettleIds) throws ActionException {
        String businessCode = funeralItem.getBusinessCode();

        // 判断历史记录编号
        if (DataUtil.invalid(historyNo)) {
            historyNo = DateUtil.formatPattern(nowDate(), "yyyyMMddHHmmss") + DataUtil.getRandomString(4);
        }

        //  判断是否有数据
        if (DataUtil.invalid(chargeSettleIds)) {
            return;
        }

        // 判断记录编号
        Integer historyId = historyChargeDao.getHistoryIdByNo(historyNo);
        if (DataUtil.valid(historyId)) {
            throw exception("历史记录编号重复");
        }

        // 历史费用记录表
        HistoryChargeEntity entity = getEntity(userItem, HistoryChargeEntity.class);
        entity.setBusinessCode(businessCode)
                .setHistoryNo(historyNo);
        historyChargeMapper.insertHistoryCharge(entity);
        if (DataUtil.invalid(entity.getId())) {
            throw exception("保存历史记录费用信息失败");
        }

        // 获取所有打印发票结算费用信息
        ArrayList<ChargeSettleViewItem> chargeSettleItems = chargeSettleDao.listChargeSettleViewBySettleIds(businessCode, chargeSettleIds);
        // 获取所有打印发票的结算费用信息集合
        ArrayList<ChargeItem> chargeItems = chargeItemDao.listChargeItemBySettleIds(businessCode, chargeSettleIds);
        // 获取所有打印发票结算付款信息集合
        ArrayList<ChargeSettlePayItem> chargeSettlePayItems = chargeSettlePayDao.listChargeSettlePayBySettleIds(businessCode, chargeSettleIds);

        // 设置字典数据
        dictionaryWrapper.resetDataText(chargeSettleItems);
        dictionaryWrapper.resetDataText(chargeItems);
        dictionaryWrapper.resetDataText(chargeSettlePayItems);

        // 保存保存费用记录
        for (ChargeSettleViewItem chargeSettleItem : chargeSettleItems) {
            // 历史结算信息
            HistoryChargeSettleEntity settleEntity = getEntity(userItem, HistoryChargeSettleEntity.class);
            BeanUtil.copy2Bean(chargeSettleItem, settleEntity);
            settleEntity.setHistoryChargeId(entity.getId());

            // 添加历史结算
            historyChargeSettleMapper.insertHistoryChargeSettle(settleEntity);
            if (DataUtil.invalid(settleEntity.getId())) {
                throw exception("保存历史记录费用结算信息失败");
            }

            // 保存历史费用详情信息
            for (ChargeItem chargeItem : chargeItems) {
                if (!DataUtil.equals(chargeItem.getChargeSettleId(), chargeSettleItem.getId())) {
                    continue;
                }
                // 历史费用详情记录
                HistoryChargeItemEntity itemEntity = getEntity(userItem, HistoryChargeItemEntity.class);
                BeanUtil.copy2Bean(chargeItem, itemEntity);
                itemEntity.setHistoryChargeId(entity.getId())
                        .setHistoryChargeSettleId(settleEntity.getId());

                // 添加历史费用详情记录
                historyChargeItemMapper.insertHistoryChargeItem(itemEntity);
                if (DataUtil.invalid(itemEntity.getId())) {
                    throw exception("保存历史记录费用详情信息失败");
                }
            }

            // 保存历史费用付款信息
            for (ChargeSettlePayItem payItem : chargeSettlePayItems) {
                if (!DataUtil.equals(payItem.getChargeSettleId(), chargeSettleItem.getId())) {
                    continue;
                }
                // 历史费用付款信息记录
                HistoryChargeSettlePayEntity payEntity = getEntity(userItem, HistoryChargeSettlePayEntity.class);
                BeanUtil.copy2Bean(payItem, payEntity);
                payEntity.setHistoryChargeId(entity.getId())
                        .setHistoryChargeSettleId(settleEntity.getId());

                // 添加历史费用付款信息记录
                historyChargeSettlePayMapper.insertHistoryChargeSettlePay(payEntity);
                if (DataUtil.invalid(payEntity.getId())) {
                    throw exception("保存历史记录费用付款信息失败");
                }
            }
        }
    }
}
